Now, you'll learn how to
style components using external Cascading Style
Sheets or CSS files as a style sheet. You can write your CSS
styling in a separate file. Just have to save the
file with the.css file extension and import
it into your application. Create a new file
called external file styling.css and insert CSS code in it as shown in the left code. Import the style sheet in your application as
shown in the right code. Class name is used instead
of a normal class in HTML files as the hypertext
markup language or HTML. The CSS styling that
you are writing inside the React
apps is not an HTML. It is a JavaScript Extensible
Markup Language or JSX, and it is passed down to the Babel compiler and
converted into JavaScript. So everything you write in HTML syntax is just JavaScript. In JavaScript, class
is a reserved keyword, so you need to use class name to add styles instead of class. If you are using
Create React app to bootstrap your app at
the time of development, you can see all
external style sheets that you have created. But if you run the
npm build command, you can see only the
index.css file because all CSS files are concatenated into a
single index.css file. With external CSS, you'll link your web pages to an
external.css file, which can be created by any
text editor on your device. The image shows the output
of styling a React component using an external style
sheet as a.css file, and you will import
it in the component. On inspecting the output, you can see that the
class names are used as you have defined
in the.css file. Each style class name should be unique in order to
apply proper styling. Here, you'll learn
the pros and cons of using an external
style sheet in React. First, here are some pros of using an external
style sheet in React. You can use the same.css
file for multiple pages. As the CSS code is in
a separate document, your HTML files will have a cleaner structure and
will be in a smaller size. To assemble styles in
complex circumstances, selector and grouping
strategies are utilized. Now, you'll learn some cons of using an external
style sheet in React. CSS class names are scoped
globally by default, which means you have to maintain a unique class name
for each styling. It leads to class
name conflicts, especially in large
style sheets. Multiple HTML elements can have numerous documents where
classes can be made. Your pages may not be rendered correctly until the
external CSS is loaded. The additional download
is expected to import documents having
style information. Uploading or linking to multiple CSS files can increase your site's
download time. To render the documents, the outer template
ought to be stacked.